#ifndef USE_RAM_ROUTINE
tempspritespace = appbackupscreen
;--------------------------------
;Large Clip Sprite
;ix->sprite
;iy->mask
;b = height
;c = width in bytes
;d = x
;e = y
LargeClippedSprite:
;Check if its even on screen
	ld a,e
	cp 64
	ret p
	add a,b
	ret m
	ret z
	ld a,d
	cp 96
	ret p
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	ret m
	ret z

	;ld a,b
	;ld (spriteheight),a
	ld a,e
	or a
	jp p,Check_clip_bottom
	neg
	push de
	ld hl,0
	ld d,l
	ld e,a
	bit 2,c
	jr z,_
	add hl,de
_
	add hl,hl	
	bit 1,c
	jr z,_
	add hl,de
_
	add hl,hl	
	bit 0,c
	jr z,_
	add hl,de
_
	pop de
	ex de,hl
	add ix,de		;Here you can save the top offset
	add iy,de
	ex de,hl
	ld e,0
	neg
	add a,b
	ld b,a
Check_clip_bottom:
	ld a,e
	add a,b
	sub 64
	jp m,Check_clip_Left
	neg
	add a,b
	ld b,a
Check_clip_Left:
;may want to save b
	xor a
	ld (bigskip),a
	ld (bigskip+1),a
	ld (Do_Clipleft),a
	ld a,d
	or a
	jp p,Check_clip_right
	cpl
	and $F8
	rra
	rra
	rra
	ex de,hl		;save the clipped left offset
	ld e,a
	ld d,0
	add ix,de
	add iy,de
	ld (bigskip),a
	ex de,hl
	inc a
	neg
	add a,c
	ld c,a
	ld a,Clipleftsize 
	ld (Do_Clipleft),a
	ld a,d
	and $07
	ld d,a
Check_clip_right:
	xor a
	ld (Do_Clipright),a
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	sub 96
	jp m,Check_clip_middle
	and $F8
	rra
	rra
	rra
	ld l,a
	ld a,(bigskip)
	add a,l
	inc a
	ld (bigskip),a
	neg
	add a,c
	ld c,a
	ld a,Cliprightsize
	ld (Do_Clipright),a
Check_clip_middle:
;this is where C should be saved.
	ld a,1
	ld (Do_ClipMiddle),a
	ld a,c
	or a
	jp nz,dontskipmiddle
	xor a
	ld (Do_ClipMiddle),a
dontskipmiddle:
	ld l,e
	ld a,d	
	ld h,0
	ld d,h
	add hl,hl
	add hl,de
	add hl,hl
	add hl,hl
	ld e,a
	and $07
	ld (BigRot),a
	push bc
	or a
	ld b,a
	ld a,$ff
	jr z,++_
_
	srl a
	djnz -_
_
	pop bc
	srl e
	srl e
	srl e
	add hl,de
	ld de,plotsscreen
	add hl,de
;this is where plotsscreen offset should be saved
	ld d,a
	cpl
	ld e,a
	;ld a,(spriteheight)
	;ld b,a
;masks should be saved to
BigSpriteRow:
	push bc
	push hl
	ld b,c
Do_Clipleft = tempspritespace
	ld a,(Do_ClipLeft)
	or a
	jr z,Clipleft
	push bc
	ld a,(bigrot)
	or a
	ld b,a
	ld a,(ix)
	ld c,(iy)
	inc ix
	inc iy
	jr z,++_
BigRot = tempspritespace+1
_
	rrca
	rrc c
	djnz -_
_
	and e
	ld b,a
	ld a,c
	and e
	or d
	ld c,a
	ld a,(hl)
	and c
	or b
	ld (hl),a
	pop bc
Clipleft:
Clipleftsize = 1
Do_ClipMiddle = tempspritespace+2
	ld a,(Do_ClipMiddle)
	or a
	jr z,ClipMiddle
BigSpriteloop:
	push bc
	ld a,(bigrot)
	or a
	ld b,a
	ld a,(ix)
	ld c,(iy)
	inc ix
	inc iy
	jr z,++_
_
	rrca
	rrc c
	djnz -_
_
	ld b,a
	push bc
		and d
		ld b,a
		ld a,c
		and d
		or e
		ld c,a
		ld a,(hl)
		and c
		or b
		ld (hl),a
		inc hl
	pop bc
	ld a,b
	and e
	ld b,a
	ld a,c
	and e
	or d
	ld c,a
	ld a,(hl)
	and c
	or b
	ld (hl),a
	pop bc
	djnz BigSpriteloop
ClipMiddle:
ClipMiddlesize = 1
Do_ClipRight = tempspritespace+3
	ld a,(Do_ClipRight)
	or a
	jr z,ClipRight
	push bc
	ld a,(bigrot)
	or a
	ld b,a
	ld a,(ix)
	ld c,(iy)
	jr z,++_
_
	rrca
	rrc c
	djnz -_
_
	and d
	ld b,a
	ld a,c
	and d
	or e
	ld c,a
	ld a,(hl)
	and c
	or b
	ld (hl),a
	pop bc
ClipRight:
Cliprightsize = 1
	pop hl
	ld bc,12			;width of the screen
	add hl,bc
bigskip = tempspritespace+4
	ld bc,(bigskip)
	add ix,bc
	add iy,bc
	pop bc
	dec b
	jp nz,bigspriterow
	ret
SpriteHeight = tempspritespace+5
#else

 relocate(appbackupscreen)
;--------------------------------
;Clip Big Sprite
;by James Montelongo
;MAX SIZE: 64x64
;ix - Sprite
;b  - height
;c  - width in bytes
;d  - x
;e  - y
LargeClippedSprite:
; Early out, Check if its even remotely on screen
	ld a,e
	cp 64
	ret p
	add a,b
	ret m
	ret z
	ld a,d
	cp 96
	ret p
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	ret m
	ret z

	ld a,e
	or a
	jp p,Check_clip_bottom
	neg
	push de
	ld hl,0
	ld d,l
	ld e,a
	bit 2,c
	jr z,$+2+1
	add hl,de
	add hl,hl	
	bit 1,c
	jr z,$+2+1
	add hl,de
	add hl,hl	
	bit 0,c
	jr z,$+2+1
	add hl,de
	pop de
	ex de,hl
	add ix,de		;Here you can save the top offset
	ex de,hl
	ld e,0
	neg
	add a,b
	ld b,a
Check_clip_bottom:

	ld a,e
	add a,b
	sub 64
	jp m,Check_clip_Left
	neg
	add a,b
	ld b,a
Check_clip_Left:
				; at this point you may want to save b
	xor a
	ld (bigskip),a
	ld a,Clipleftsize 
	ld (Do_Clipleft),a
	ld a,d
	or a
	jp p,Check_clip_right
	cpl
	and $F8
	rra
	rra
	rra
	ex de,hl		;save the clipped left offset
	ld e,a
	ld d,0
	add ix,de
	ld (bigskip),a
	ex de,hl
	inc a
	neg
	add a,c
	ld c,a
	xor a
	ld (Do_Clipleft),a
	ld a,d
	and $07
	ld d,a
Check_clip_right:

	ld a,Cliprightsize 
	ld (Do_Clipright),a
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	sub 96
	jp m,Check_clip_middle
	and $F8
	rra
	rra
	rra
	ld l,a
	ld a,(bigskip)
	add a,l
	inc a
	ld (bigskip),a
	neg
	add a,c
	ld c,a
	xor a
	ld (Do_Clipright),a
Check_clip_middle:
				; This is where C should be saved.
	xor a
	ld (Do_ClipMiddle),a
	ld a,c
	or a
	jp nz,dontskipmiddle
	ld a,ClipMiddlesize 
	ld (Do_ClipMiddle),a
dontskipmiddle:
	ld l,e
	ld a,d	
	ld h,0
	ld d,h
	add hl,hl
	add hl,de
	add hl,hl
	add hl,hl
	ld e,a
	and $07
	xor 7
	ld (BigRot1),a
	ld (BigRot2),a
	ld (BigRot3),a
	add a,a
	ld (clipbigrot1),a
	ld a,$ff
clipbigrot1 = $+1
	jr $
	srl a
	srl a
	srl a
	srl a
	srl a
	srl a
	srl a
	srl e
	srl e
	srl e
	add hl,de
	ld de,plotsscreen
	add hl,de
				; This is where gbuf offset should be saved.
	ld d,a
	cpl
	ld e,a
				;masks should be saved to
BigSpriteRow:
	push bc
	push hl
	ld b,c
Do_Clipleft = $+1
	jr Clipleft
	ld a,(ix)
	inc ix
BigRot1 = $+1
	jr $
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
BigMask0:
	and e
	or (hl)
	ld (hl),a
Clipleft:
Clipleftsize = Clipleft-(Do_Clipleft+1)

Do_ClipMiddle = $+1
	jr $+2
BigSpriteloop:
	ld a,(ix)
	inc ix
BigRot2 = $+1
	jr $
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	ld c,a
BigMask1:
	and d
	or (hl)
	ld (hl),a
	inc hl
	ld a,c
BigMask2:
	and e
	or (hl)
	ld (hl),a
	djnz BigSpriteloop
ClipMiddle:
ClipMiddlesize = ClipMiddle-(Do_ClipMiddle+1)

Do_ClipRight = $+1
	jr ClipRight
	ld a,(ix)
BigRot3 = $+1
	jr $
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
BigMask3:
	and d
	or (hl)
	ld (hl),a
ClipRight:
Cliprightsize = ClipRight-(Do_ClipRight+1)
	pop hl

	ld bc,12			;width of the screen
	add hl,bc

bigskip = $+1
	ld bc,0
	add ix,bc
	pop bc
	djnz BigSpriteRow
	ret
 endrelocate()

;--------------------------------
;Clip Big Sprite
;by James Montelongo
;MAX SIZE: 64x64
;ix - Sprite
;b  - height
;c  - width in bytes
;d  - x
;e  - y
RamCodeStart:
; Early out, Check if its even remotely on screen
	ld a,e
	cp 64
	ret p
	add a,b
	ret m
	ret z
	ld a,d
	cp 96
	ret p
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	ret m
	ret z

	ld a,e
	or a
	jp p,Check_clip_bottom
	neg
	push de
	ld hl,0
	ld d,l
	ld e,a
	bit 2,c
	jr z,$+2+1
	add hl,de
	add hl,hl	
	bit 1,c
	jr z,$+2+1
	add hl,de
	add hl,hl	
	bit 0,c
	jr z,$+2+1
	add hl,de
	pop de
	ex de,hl
	add ix,de		;Here you can save the top offset
	ex de,hl
	ld e,0
	neg
	add a,b
	ld b,a
	ld a,e
	add a,b
	sub 64
	jp m,Check_clip_Left
	neg
	add a,b
	ld b,a				; at this point you may want to save b
	xor a
	ld (bigskip),a
	ld a,Clipleftsize 
	ld (Do_Clipleft),a
	ld a,d
	or a
	jp p,Check_clip_right
	cpl
	and $F8
	rra
	rra
	rra
	ex de,hl		;save the clipped left offset
	ld e,a
	ld d,0
	add ix,de
	ld (bigskip),a
	ex de,hl
	inc a
	neg
	add a,c
	ld c,a
	xor a
	ld (Do_Clipleft),a
	ld a,d
	and $07
	ld d,a
	ld a,Cliprightsize 
	ld (Do_Clipright),a
	ld a,c
	add a,a
	add a,a
	add a,a
	add a,d
	sub 96
	jp m,Check_clip_middle
	and $F8
	rra
	rra
	rra
	ld l,a
	ld a,(bigskip)
	add a,l
	inc a
	ld (bigskip),a
	neg
	add a,c
	ld c,a
	xor a
	ld (Do_Clipright),a				; This is where C should be saved.
	xor a
	ld (Do_ClipMiddle),a
	ld a,c
	or a
	jp nz,dontskipmiddle
	ld a,ClipMiddlesize 
	ld (Do_ClipMiddle),a
	ld l,e
	ld a,d	
	ld h,0
	ld d,h
	add hl,hl
	add hl,de
	add hl,hl
	add hl,hl
	ld e,a
	and $07
	xor 7
	ld (BigRot1),a
	ld (BigRot2),a
	ld (BigRot3),a
	add a,a
	ld (clipbigrot1),a
	ld a,$ff
	jr $
	srl a
	srl a
	srl a
	srl a
	srl a
	srl a
	srl a
	srl e
	srl e
	srl e
	add hl,de
	ld de,plotsscreen
	add hl,de		; This is where gbuf offset should be saved.
	ld d,a
	cpl
	ld e,a			;masks should be saved to
BigSpriteRow2:
	push bc
	push hl
	ld b,c
	jr _
	ld a,(ix)
	inc ix
	jr $
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	and e
	or (hl)
	ld (hl),a
_
	jr $+2
BigSpriteloop2:
	ld a,(ix)
	inc ix
	jr $
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	ld c,a
	and d
	or (hl)
	ld (hl),a
	inc hl
	ld a,c
	and e
	or (hl)
	ld (hl),a
	djnz BigSpriteloop2
_
	jr _
	ld a,(ix)
	jr $
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	rrca
	and d
	or (hl)
	ld (hl),a
_
	pop hl

	ld bc,12			;width of the screen
	add hl,bc
	ld bc,0
	add ix,bc
	pop bc
	djnz BigSpriteRow2
	ret
RamCodeEnd
#endif
